package com.bobo.controller.back.message;

import com.alibaba.druid.util.StringUtils;
import com.bobo.annotations.RequestBody;
import com.bobo.annotations.RequestMapping;
import com.bobo.controller.base.BaseServlet;
import com.bobo.dto.Page;
import com.bobo.model.Message;
import com.bobo.service.message.IMessageService;
import com.bobo.service.message.MessageServiceImpl;
import com.sun.org.apache.xml.internal.serializer.utils.Utils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.sun.org.apache.xml.internal.serializer.utils.Utils.messages;

/**
 * Created by bobo on 16-10-30.
 */
@WebServlet(name = "Message",urlPatterns = {"/admin/message/*"})
public class MessageBcak extends BaseServlet{

    IMessageService messageService = new MessageServiceImpl();

    @RequestMapping("/getMessages")
    public String getMessages(HttpServletRequest request) {
        int current = 1;
        int pageSize = 5;
        int status = 0;

        if (request.getParameter("current") != null && !StringUtils.equals(request.getParameter("current"), "")) {
            current = Integer.valueOf(request.getParameter("current"));
        }
        if (request.getParameter("pageSize") != null && !"".equals(request.getParameter("pageSize"))) {
            pageSize = Integer.valueOf(request.getParameter("pageSize"));
        }
        if (request.getParameter("status") != null && !"".equals(request.getParameter("status"))){
            status = Integer.valueOf(request.getParameter("status"));
        }
        Page<Message> page = messageService.getMessages(current, pageSize, status);
        request.setAttribute("page",page);
        return "jsp/back/message/getMessages";

    }

    @RequestBody
    @RequestMapping("/changeStatus")
    public String changeStatus(HttpServletRequest request){
        int id = Integer.parseInt(request.getParameter("id"));
        Message message = messageService.getById(id);
        int status = Integer.parseInt(request.getParameter("status"));
        message.setStatus(status);
        messageService.upd(message);
        return "true";
    }

    @RequestBody
    @RequestMapping("/del")
    public String del(HttpServletRequest request){
        int id = Integer.parseInt(request.getParameter("id"));
        messageService.del(id);
        return "true";
    }
}
